package com.tengju.bff.interfaces.app.csa.facade;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tengju.bff.interfaces.app.csa.assembler.GenSecCustomerServiceInfoAssembler;
import com.tengju.bff.interfaces.app.csa.vo.GenSecCustomerServiceGreetVO;
import com.tengju.bff.interfaces.app.csa.vo.GenSecCustomerServiceInfoVO;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.user.application.user.UserDocumentService;
import com.tengju.user.domain.model.csa.ClubCustomerServiceInfo;
import com.tengju.user.domain.model.csa.GenSecCustomerServiceInfoRepository;
import com.tengju.user.domain.model.doc.UserDocument;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.model.user.UserInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: kezunfa
 * @Date: 2021/4/20 13:09
 * @Description: 
 */
@Slf4j
@Component
public class GenSecCustomerServiceInfoFacade {

    private UserDocumentService userDocumentService;

    private UserInfoRepository userInfoRepository;

    private GenSecCustomerServiceInfoRepository clubCustomerServiceInfoRepository;

    @Value("${gen.sec.customer.service.name}")
    String genSecCustomerServiceName;//客服名称

    @Value("${gen.sec.customer.service.wechat}")
    String genSecCustomerServiceWechat;//客服微信

    @Value("${gen.sec.customer.service.head.image}")
    String genSecCustomerServiceHeadImage;//客服头像

    @Value("${gen.sec.customer.service.worl_date}")
    String genSecCustomerServiceWorkDate;//客服服务时间

    @Value("${gen.sec.customer.service.customer.service.greet.json}")
    String genSecCustomerServiceWorkGreetJson;//打招呼模板

    @Autowired
    public GenSecCustomerServiceInfoFacade(UserDocumentService userDocumentService, UserInfoRepository userInfoRepository, GenSecCustomerServiceInfoRepository clubCustomerServiceInfoRepository) {
        this.userDocumentService = userDocumentService;
        this.userInfoRepository = userInfoRepository;
        this.clubCustomerServiceInfoRepository = clubCustomerServiceInfoRepository;
    }

    public GenSecCustomerServiceInfoVO getClubCustomerServiceInfo() {
        UserServletContext userServletContext = ServletContextUtil.getServletContext();
        UserInfo userInfo = userInfoRepository.getByUserId(userServletContext.needUserId());
        //如果用户信息为空，返回空的vo
        GenSecCustomerServiceInfoVO genSecCustomerServiceInfoVO = new GenSecCustomerServiceInfoVO();
        // 客服默认信息
        genSecCustomerServiceInfoVO.setCsaName(genSecCustomerServiceName);
        genSecCustomerServiceInfoVO.setCsaWechat(genSecCustomerServiceWechat);
        genSecCustomerServiceInfoVO.setHeadImage(genSecCustomerServiceHeadImage);
        List<GenSecCustomerServiceGreetVO> genSecCustomerServiceGreetVOList = JSON.parseObject(genSecCustomerServiceWorkGreetJson,new TypeReference<ArrayList<GenSecCustomerServiceGreetVO>>(){});
        genSecCustomerServiceInfoVO.setGenSecCustomerServiceGreetVOList(genSecCustomerServiceGreetVOList);
        genSecCustomerServiceInfoVO.setServiceWorkTime(genSecCustomerServiceWorkDate);

        if (userInfo == null) {
            return genSecCustomerServiceInfoVO;
        }
        UserDocument userDocument = userDocumentService.findById(userInfo.getUserIdCode());
        if (userDocument == null) {
            return genSecCustomerServiceInfoVO;
        }
        ClubCustomerServiceInfo clubCustomerServiceInfo = clubCustomerServiceInfoRepository.getBySecIdCode(UserIdCode.of(userDocument.getOwnerGenSecIdCode()));
        return clubCustomerServiceInfo == null ? genSecCustomerServiceInfoVO : GenSecCustomerServiceInfoAssembler.clubCustomerServiceToVO(clubCustomerServiceInfo);
    }
}
